import ApiSts from "@/api/StsSignUserDir_get";
import OSSClient from "ali-oss";
export class OSSHelper {
  private _inner?: OSSClient;
  put(account: string, dest: string, blob: Blob): Promise<OSSClient.PutObjectResult> {
    return ApiSts.call({ wechatNumber: account }).then(r => {
      return r.data as Required<ApiSts.RespData>
    }).then((data) => {
      this._inner = new OSSClient({
        endpoint: data.endPoint,
        accessKeyId: data.accessKeyId,
        accessKeySecret: data.accessKeySecret,
        bucket: data.bucketName,
        stsToken: data.securityToken,
      })
      const object_name = `${data?.dir}/${dest}`.replace(/\//, '/').replace(/\/\//, '/')
      return this._inner.put(object_name, blob, {
        mime: blob.type,
        headers: {
          'Content-Type': blob.type
        }
      })
    })
  }
}
export const oss_helper = new OSSHelper()